<template>
  <layout>
    <Tabs class-prefix="type" :dataSource="typeList" :value.sync="type" />
    <div>
      <ol v-if="groupList.length > 0">
        <li v-for="(group, index) in groupList" :key="index">
          <h3 class="title">
            {{ beautify(group.title) }}<span>￥{{ group.total }}</span>
          </h3>
          <ol>
            <li v-for="item in group.items" :key="item.id" class="record">
              <span>{{ tagString(item.tags) }}</span>
              <span class="notes">{{ item.notes }}</span>
              <span>￥{{ item.amount }} </span>
            </li>
          </ol>
        </li>
      </ol>
      <div v-else class="noResult">目前没有相关记录</div>
    </div>
  </layout>
</template>

<script lang="ts">
import { Vue, Component } from "vue-property-decorator";
import Tabs from "@/components/Tabs.vue";
import { typeList } from "@/constants/typeList";
import dayjs from "dayjs";
import { clone } from "@/lib/clone";

@Component({
  components: { Tabs },
})
export default class Statistics extends Vue {
  type = "-";
  typeList = typeList;
  tagString(tags: Tag[]): any {
    return tags.length === 0 ? "无" : tags.map((t) => t.name).join(",");
  }
  beforeCreate(): any {
    this.$store.commit("fetchRecords");
  }
  get recordList(): RecordItem[] {
    return (this.$store.state as RootState).recordList;
  }
  get groupList(): any {
    const { recordList } = this;
    const newList = clone(recordList)
      .filter((r) => r.type === this.type)
      .sort(
        (a, b) => dayjs(b.createdAt).valueOf() - dayjs(a.createdAt).valueOf()
      );
    if (newList.length === 0) {
      return [] as Result;
    }
    type Result = { title: string; total?: number; items: RecordItem[] }[];
    const result: Result = [
      {
        title: dayjs(newList[0].createdAt).format("YYYY-MM-DD"),
        items: [newList[0]],
      },
    ];
    for (let i = 1; i < newList.length; i++) {
      const current = newList[i];
      const last = result[result.length - 1];
      if (dayjs(last.title).isSame(dayjs(current.createdAt), "day")) {
        last.items.push(current);
      } else {
        result.push({
          title: dayjs(current.createdAt).format("YYYY-MM-DD"),
          items: [current],
        });
      }
    }
    result.map((group) => {
      group.total = group.items.reduce((sum, item) => {
        return sum + item.amount;
      }, 0);
    });
    return result;
  }

  beautify(string: string): any {
    const now = dayjs();
    const day = dayjs(string);
    if (day.isSame(now, "day")) {
      return "今天";
    } else if (day.isSame(now.subtract(1, "day"), "day")) {
      return "昨天";
    } else if (day.isSame(now.subtract(2, "day"), "day")) {
      return "前天";
    } else if (day.isSame(now, "year")) {
      return day.format("M月D日");
    } else {
      return day.format("YYYY年M月D日");
    }
  }
}
</script>

<style lang="scss" scoped>
.noResult {
  padding: 16px;
  text-align: center;
}
%item {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.title {
  @extend %item;
  padding: 8px 16px;
  line-height: 24px;
}
.record {
  @extend %item;
  font-size: 16px;
  background: #fff;
  padding: 2px 16px;
  border-bottom: 1px solid #ddd;
}
.notes {
  font-size: 14px;
  margin-right: auto;
  margin-left: 10px;
  color: #999;
}
</style>